package org.example.examfx02.controller;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import org.example.examfx02.entity.CourseChoose;
import org.example.examfx02.entity.CourseRequire;
import org.example.examfx02.entity.Teacher;
import org.example.examfx02.HelloApplication;
import org.example.examfx02.tool.StringUtil;
import org.example.examfx02.tool.SystemData;

import java.util.ArrayList;


public class AddViewController {
    public  static SystemData sys = SystemData.getInstance();
    @FXML
    Label label;
    @FXML
    TextField textId;
    @FXML
    TextField textCourseName;
    @FXML
    TextField textNum;
    @FXML
    TextField text;
    @FXML
    Button admitBtn;
    @FXML
    RadioButton radioRequire;
    @FXML
    RadioButton radioChoose;
    @FXML
    ChoiceBox tChoiceBox;

    public void admit(){

       String id = textId.getText();
       if(StringUtil.isEmpty(id)){
           HelloApplication.showAlert("警告","ID不能为空！");
           return;
       }
       String name = textCourseName.getText();
       if(StringUtil.isEmpty(name)){
            HelloApplication.showAlert("警告","姓名不能为空！");
            return;
       }
       int chooseNum,typeChoose;
       try{
           chooseNum = Integer.parseInt(textNum.getText());
           typeChoose = Integer.parseInt(text.getText());
           String teacherInfo = tChoiceBox.getValue().toString();
           Teacher t = sys.getUserGroup().getTeacherByInfo(teacherInfo);
           if(t==null){
               HelloApplication.showAlert("警告","请检查教师！");
           }else{
               if(radioRequire.isSelected()){
                   if(sys.FindCoursebyID(id)==null||sys.FindCoursebyID(id).size()==0){
                       CourseRequire course = new CourseRequire(id,name,chooseNum,t.getWorkId(),typeChoose);
                       sys.getCourses().add(course);
                   }else{
                       HelloApplication.showAlert("警告","ID不可重复！");
                   }
               }else{
                   if(sys.FindCoursebyID(id)==null||sys.FindCoursebyID(id).size()==0){
                       CourseChoose course = new CourseChoose(id,name,chooseNum,t.getWorkId(),typeChoose);
                       sys.getCourses().add(course);
                   }else{
                       HelloApplication.showAlert("警告","ID不可重复！");
                   }
               }
           }
       }catch (NumberFormatException e){
           HelloApplication.showAlert("警告","类型或选课人数存在异常！");
       }catch(Exception e){
           HelloApplication.showAlert("警告","请检查教师！");
       }

    }
    public void initialize(){
        ToggleGroup group = new ToggleGroup();
        radioChoose.setToggleGroup(group);
        radioRequire.setToggleGroup(group);
        ArrayList<String> teacherInfo = sys.getUserGroup().getTeachersInfo();
        tChoiceBox.setItems(FXCollections.observableArrayList(teacherInfo));
    }
    public void changeRadioLabel(){
        if(radioRequire.isSelected()){
            label.setText("学分：");
        }else{
            label.setText("限选人数：");
        }
    }

}
